package cn.com.capinfo.ea.example.demo.service



import cn.com.capinfo.ea.example.demo.domain.Book
import cn.com.capinfo.ea.core.PageParams
import groovy.util.logging.Slf4j
import org.grails.datastore.mapping.query.api.BuildableCriteria
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
/**
 * Generate by CapinfoTools EA-3.7.0 on 2020-07-27.
 */
@Slf4j
@Service
@Transactional
class BookService {
    public long count(Closure logic){
        BuildableCriteria buildableCriteria=Book.createCriteria()
        logic.delegate=buildableCriteria
        long count= buildableCriteria.count{
            logic()
        }
        return count
    }
    public List list(PageParams pageParams, Closure logic){
        BuildableCriteria buildableCriteria=Book.createCriteria()
        logic.delegate=buildableCriteria
        List list = buildableCriteria.list {
            logic()
            order(pageParams.sort, pageParams.order)
            maxResults(pageParams.max)
            firstResult(pageParams.offset)
        }
        return list;
    }
    public Book create(Map map=[:]){
        return new Book(map)
    }
    public Book get(long id){
        return Book.get(id)
    }
    public Book read(long id){
        return Book.read(id)
    }
    public boolean save(Book book,boolean flush=true){
        return book.save(flush:flush)
    }
    public boolean delete(Book book,boolean flush=true){
        Book.withTransaction { status ->
            try {
                book.delete(flush:flush);
                return true
            } catch (Exception e) {
                status.setRollbackOnly()
                return false
            }
        }
    }
    public boolean deletes(String ids,boolean flush=true){
        List list = ids?.tokenize(',')?.unique()?.collect { it.toLong()};
        Book.withTransaction { status ->
            try {
                Book.where {
                    id in list
                }.deleteAll();
                return true;
            } catch (e) {
                status.setRollbackOnly()
                return false
            }
        }
    }
}
